<?php
class Category extends AppModel {

	var $name = 'Category';

	var $validate = array(
		'name' => array(
			'rule' => 'notEmpty'
		)
	);

	function getTotals($period = null, $ids = null) {

		$q = 'SELECT category_id, sum(amount) as total ';
		$q .= 'FROM purchases ';
		if (is_array($ids)) {
			$q .= 'WHERE category_id IN ('.implode(',',$ids).') ';
		}
		$q .= 'GROUP BY category_id';

		$data = $this->query($q);

		$ids = Set::extract($data, '{n}.purchases.category_id');
		$tots = Set::extract($data, '{n}.0.total');

		$data = array_combine($ids, $tots);

		return $data;
	}

	function afterSave() {

	}

}
?>
